package com.manli.api.service;

import com.manli.api.pojo.QueryParamsArea;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.List;
import com.manli.api.pojo.Result;
import com.manli.api.util.ResultUtil;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import com.manli.api.pojo.QueryParams;
import com.manli.api.dao.ACitiesDao;
import com.manli.api.dto.ACities;
import org.springframework.transaction.annotation.Transactional;


@Service("aCitiesService")
public class ACitiesService {
    @Autowired
    ACitiesDao aCitiesDao;
    @Autowired
    SQLManager sqlManager;

    
    public Result queryPage(QueryParams params) {
        /**
        * sql中表名需要修改
        */
        StringBuffer sql = new StringBuffer("select * from aCities where 1=1 ");

        SQLReady sqlReady = new SQLReady(sql.toString());

        PageQuery<ACities> pageQuery = new PageQuery<ACities>(params.getPage(),params.getLimit());
        PageQuery<ACities> query =sqlManager.execute(sqlReady,ACities.class,pageQuery);
        return ResultUtil.success(query.getList(),query.getTotalRow());
    }

    
    public Result queryAll(QueryParamsArea provinceCode) {
        StringBuffer sql = new StringBuffer("select * from a_cities where 1=1 ");
        sql.append(StringUtils.isEmpty(provinceCode.getProvinceCode())?"":"and province_code = " + provinceCode.getProvinceCode());
        List<ACities> list = aCitiesDao.execute(sql.toString());
        return ResultUtil.success(list);
    }

    
    public Result getById(String code) {
        ACities aCities = aCitiesDao.single(code);
        return ResultUtil.success(aCities);
    }

    @Transactional
    public Result save(ACities aCities) {
        aCitiesDao.insert(aCities);
        return ResultUtil.success();
    }

    @Transactional
    public Result updateById(ACities aCities) {
        aCitiesDao.updateTemplateById(aCities);
        return ResultUtil.success();
    }
    @Transactional
	public Result updateTemplateById(ACities aCities) {
        aCitiesDao.updateTemplateById(aCities);
        return ResultUtil.success();
    }
    @Transactional
    public Result removeById(String code) {
        aCitiesDao.deleteById(code);
        return ResultUtil.success();
    }

    @Transactional
    public Result removeByIds(List<String> ids) {
        for (String id:ids) {
            aCitiesDao.deleteById(id);
        }
        return ResultUtil.success();
    }



}
